package com.newpage.module_gather.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.newpage.module_gather.R;
import com.wetao.lib_common.bean.response.RevisionExamQuestionInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * author: zhouyh
 * created on: 2021/6/22 6:12 下午
 * description: 错题订正详情题号adapter
 */
public class QuestionNoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private OnItemClickListener onItemClickListener;
    private Context mContext;
    private List<RevisionExamQuestionInfo.Data.QuestionListBean> questionListBeans = new ArrayList<>();//题目列表
    private int checkPos;

    public void notifyDataChanged() {
        notifyDataSetChanged();
    }


    //设置试题
    public void setQuestionData(List<RevisionExamQuestionInfo.Data.QuestionListBean> datas) {
        if (datas != null) {
            this.questionListBeans.clear();
            this.questionListBeans.addAll(datas);
            notifyDataChanged();
        }
    }

    public QuestionNoAdapter(Context context) {
        mContext = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(mContext).inflate(R.layout.gather_question_no_item, parent, false);
        return new QuestionViewHolder(view);

    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, @SuppressLint("RecyclerView") final int position) {
        if (holder instanceof QuestionViewHolder) {

            ((QuestionViewHolder) holder).tv_question_no.setText("" + questionListBeans.get(position).getQuestionNo());



            if (getCheckPos() == position) {
                ((QuestionViewHolder) holder).tv_question_no.setTextColor(mContext.getResources().getColor(R.color.white));
                ((QuestionViewHolder)holder).tv_question_no.setBackground(mContext.getResources().getDrawable(R.drawable.common_question_no_select));
                if (questionListBeans.get(position).getIsRight()!=null && questionListBeans.get(position).getIsRight() != 1){
                    ((QuestionViewHolder) holder).dot_view.setVisibility(View.VISIBLE);
                }else{
                    ((QuestionViewHolder) holder).dot_view.setVisibility(View.INVISIBLE);
                }
            } else {
                ((QuestionViewHolder)holder).tv_question_no.setTextColor(mContext.getResources().getColor(R.color.wifi_Text));
                ((QuestionViewHolder)holder).tv_question_no.setBackground(mContext.getResources().getDrawable(R.drawable.common_question_no_normal));
                if (questionListBeans.get(position).getIsRight()!=null && questionListBeans.get(position).getIsRight() != 1){
                    ((QuestionViewHolder) holder).dot_view.setVisibility(View.VISIBLE);
                    ((QuestionViewHolder)holder).tv_question_no.setTextColor(mContext.getResources().getColor(R.color.understand_text_red));
                }else {
                    ((QuestionViewHolder) holder).dot_view.setVisibility(View.INVISIBLE);
                    ((QuestionViewHolder)holder).tv_question_no.setTextColor(mContext.getResources().getColor(R.color.wifi_Text));
                }
                if (questionListBeans.get(position).getIsRevision()!=null&&questionListBeans.get(position).getIsRevision() == 1){
                    ((QuestionViewHolder)holder).tv_question_no.setTextColor(mContext.getResources().getColor(R.color.bar_color));
                }
            }

            ((QuestionViewHolder) holder).tv_question_no.setOnClickListener(v->{
                if (onItemClickListener!=null){
                    onItemClickListener.onItemClick(position);
                    setCheckPos(position);
                    notifyDataChanged();
                }
            });


        }

    }

    @Override
    public int getItemCount() {
        return questionListBeans.size();
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }


    //试题列表
    class QuestionViewHolder extends RecyclerView.ViewHolder {

        private TextView tv_question_no;
        private ImageView dot_view;


        public QuestionViewHolder(View itemView) {
            super(itemView);

            tv_question_no = itemView.findViewById(R.id.tv_question_item_no);
            dot_view = itemView.findViewById(R.id.dot_view);

        }
    }

    public int getCheckPos() {
        return checkPos;
    }

    public void setCheckPos(int checkPos) {
        this.checkPos = checkPos;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position);

    }
}
